/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClientSide;

import java.io.*;
import java.util.*;
import model.*;
import controller.ControllerWD;
import java.util.logging.*;
import helper.*;

/**
 *
 * @author ImNotPro
 */
public class LastModifiedChecker extends Thread {

    
    private ArrayList<Long> timeModified = new ArrayList<Long>();
    private ArrayList<Long> temptimeModified = new ArrayList<Long>();
    private String username;
    private ArrayList<WorkingDirectory> WD = new ArrayList<WorkingDirectory>();
    private int delay = 10;

    /**
     * @param aWD the WD to set
     */
    public void setWD(ArrayList<WorkingDirectory> aWD) {
        WD = aWD;
    }

    @Override
    public void run() {
        for (int i = 0; i < WD.size(); ++i) {
            InitLastUpdate(WD.get(i).getDir(), WD.get(i).getPath());
        }
        try {
            Thread.sleep(delay * 1500);//waiting Revision Checker
        } catch (InterruptedException ex) {
            Logger.getLogger(LastModifiedChecker.class.getName()).log(Level.SEVERE, null, ex);
        }
        while (true) {
            CheackUpdate();
            try {
                Thread.sleep(delay*1500);
            } catch (InterruptedException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void CheackUpdate() {
        temptimeModified = new ArrayList<Long>();
        for (int i = 0; i < WD.size(); ++i) {
            GetAllLastUpdate(WD.get(i).getDir(), WD.get(i).getPath());
        }
        if (!CompareLastModified(timeModified, temptimeModified)) {
            timeModified = temptimeModified;
            System.out.println("Change!!!");

            ControllerWD.WDclientMaker(WD);
            SentWDtoServer(WD);
        } else {
            System.out.println("No Change");
        }
    }

    private boolean CompareLastModified(ArrayList<Long> x, ArrayList<Long> y) {
        if (x.size() != y.size()) {
            return false;
        } else {
            for (int i = 0; i < x.size(); ++i) {
                long a = x.get(i);
                long b = y.get(i);
                if (a != b) {
                    return false;
                }
            }
        }
        return true;
    }

    private void GetAllLastUpdate(String dirc, String path) {
        String loc = path + "/" + dirc;
        File dir = new File(loc);
        String[] children = dir.list();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            for (int i = 0; i < children.length; i++) {
                String filename = children[i];
                File file = new File(loc + "/" + filename);
                if (!file.isHidden()) {
                    if (file.isDirectory()) {
                        GetAllLastUpdate(dirc + "/" + file.getName(), path);
                    } else {
                        temptimeModified.add(file.lastModified());
                    }
                }
            }
        }
    }

    private void InitLastUpdate(String dirc, String path) {
        String loc = path + "/" + dirc;
        File dir = new File(loc);
        String[] children = dir.list();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            for (int i = 0; i < children.length; i++) {
                String filename = children[i];
                File file = new File(loc + "/" + filename);
                if (!file.isHidden()) {
                    if (file.isDirectory()) {
                        InitLastUpdate(dirc + "/" + file.getName(), path);
                    } else {
                        timeModified.add(file.lastModified());
                    }
                }
            }
        }
    }

    private void SentWDtoServer(ArrayList<WorkingDirectory> WD) {
        try {
            Connect.sendStructure(getUsername(), WD);
        } catch (Exception ex) {
            Logger.getLogger(LastModifiedChecker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    
}
